<?php 
session_start();
if (isset($_POST['dt']) && isset($_POST['tiku']) && isset($_POST['fdes'])){
    $openid=$_POST['fdes'];
	$dt=trim($_POST['dt']);
    $tiku=$_POST['tiku'];
	include_once("../c/config.php");
	$mysqli = new mysqli($dbhost, $dbuser, $dbpwd, $dbName);
	if (!$mysqli->set_charset("utf8")) {printf("Error loading character set utf8: %s\n", $mysqliTwo->error);}
	
	$fileName=3;
	if (isset($_SESSION['nianji'])){
	    $fileName=$_SESSION['nianji'];
	}
	$getFile='../'.$fileName.'c.txt';
	$tiDataInfo=json_decode(file_get_contents($getFile), true); //考题答案
	$tiData=$tiDataInfo['d'];
	if (empty($tiDataInfo)){
		$tiData=array();
		$sql="select id,dan from ti ";
		$query = $mysqli->query($sql);
		while ($list=$query->fetch_array()){
			$tiData[$list['id']]=array('dan'=>$list['dan']);  
		}
		file_put_contents($getFile, json_encode(array('d'=>$tiData)));
	}else{
		$tiData=$tiDataInfo['d'];
	}
	$dati=json_decode($dt);
	$tikuTmp=json_decode($tiku);
	$dadui=0; //回答正确的提数
	$fenshu=0; //考生分数
	foreach ($dati as $k => $v){
	    if (!empty($v)){
	        if ($tiData[$tikuTmp[$k]]['dan'] == $v){
	            $fenshu++;
	            $dadui++;
	        }
	    }
	}
	$query=$mysqli->query("select count(id) as count from ci3 where openid='".$openid."' limit 0, 1");
	$countInfo=$query->fetch_assoc();
	
	if (isset($_SESSION['skey'])){}else{
	    $_SESSION['skey']=rand(1,99999999);
	}
	
	if($countInfo['count'] > 2){
	    $tableName='ci3_more';
	    $mysqli->query("insert into ".$tableName."(fen,openid,dati,tiku,createtime) values(".$fenshu.",'".$openid."','".$dt."','".$tiku."','".date("Y-m-d H:i:s")."') ");
	    
	}else{
	    
	    $skey=$_SESSION['skey'];
	    $sql="select id from ci3 where openid='".$openid."' and skey=".$skey."  limit 0, 1";
	    $query=$mysqli->query($sql);
	    if($query->num_rows>0){
	        
	    }else{
	        $mysqli->query("insert into ci3(skey,fen,openid,dati,tiku,createtime) values(".$skey.",".$fenshu.",'".$openid."','".$dt."','".$tiku."','".date("Y-m-d H:i:s")."') ");
	    }
	    
	    
	}
	
	
	
	
	$mysqli->close();
}
echo json_encode(array('status'=>'ok'));

?>